<?php   // $Id: backup_creer.php 259 2008-08-10 13:35:40Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors: Pierre Coustillas
*/
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/utils.php");

$file=isset($_POST['nom_backup'])?$_POST['nom_backup']:"backup.sql";


$entete = "-- ----------------------\n";
$entete .= "-- dump de la base $db au ".date("d-M-Y")."\n";
$entete .= "-- ----------------------\n\n\n";
$creations = "-- Outil de backup de OpenDCF --";
$insertions = "\n\n";
$suppr="\n\n";

$listeTables = mysql_query("show tables");
while($table = mysql_fetch_array($listeTables))
{
	$tbl=$table[0];

	$suppr .="DROP TABLE IF EXISTS $tbl;--FIN--\n";



	$creations .= "\n-- ----- Creation de la table $tbl -------\n";
	$result_creations_tables = mysql_query("show create table $tbl");
	$creations .= mysql_result($result_creations_tables,0,1).";--FIN--\n\n";
	/*while($creationTable = mysql_fetch_array($listeCreationsTables))
	{
		$creations .= $creationTable[1].";\n\n";
	}*/

	$donnees = mysql_query("SELECT * FROM $tbl");
	$insertions .= "-- ----- Insertions dans la table $tbl -------\n";

	while($nuplet = mysql_fetch_array($donnees))
	{
 			$insertions .= "INSERT INTO $tbl VALUES(";
			for($i=0; $i < mysql_num_fields($donnees); $i++)
			{
			if($i != 0)
				$insertions .=  ", ";
			$type_champ=mysql_field_type($donnees, $i);
			if($type_champ == "string" || $type_champ == "blob"|| $type_champ == "date")
				$insertions .=  "'";
			$insertions .= addslashes($nuplet[$i]);
			if($type_champ == "string" || $type_champ == "blob"|| $type_champ == "date")
				$insertions .=  "'";
			}
			$insertions .=  ");--FIN--\n";
	}
	$insertions .= "\n";

}


$fichierDump = fopen("dump/$file", "wb");
fwrite($fichierDump, $entete);
fwrite($fichierDump, $suppr);
fwrite($fichierDump, $creations);
fwrite($fichierDump, $insertions);
fclose($fichierDump);

header("Location: backup_accueil.php?message=Sauvegarde réalisée avec succès !");


?>